<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="投题标题" prop="title">
				<el-input v-model="form.title" placeholder="投题标题" clearable :maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="投票类型" prop="voteType">
						<sc-select v-model="form.voteType" :params="{code:'voteType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="发布部门" prop="sendDeptId">
						<select-dept-dialog v-model:deptId="form.sendDeptId" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="人员权限" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="部门权限">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="职务权限">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="起始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="起始时间" type="date" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="投标方式" prop="isAnonymous">
						<el-radio-group v-model="form.isAnonymous">
							<el-radio-button label="1">实名</el-radio-button>
							<el-radio-button label="0">匿名</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="查看结果" prop="readRes">
						<el-radio-group v-model="form.readRes">
							<el-radio-button label="0">允许</el-radio-button>
							<el-radio-button label="1">禁止</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<voteItem v-model:listData="itemJson"></voteItem>
			<el-form-item label="投票内容">
				<sc-editor v-model="form.content" placeholder="请输入" :templates="templates" :height="300" clearable style="width: 100%;"></sc-editor>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'vote'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="vote"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import voteItem from './voteItem'
import AttachMenu from '@/views/module/attachMenu'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		voteItem,
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '创建投票',
				edit: '编辑投票',
			},
			templates: [],
			fileList: [],
			itemJson:[],
			apiObj: this.$API.file.attach.uploadFile,
			form: {
				voteId: '',
				title: '',
				voteType: '',
				sendDeptId: '',
				userRole: '',
				deptRole: '',
				levelRole: '',
				isAnonymous: "1",
				readRes: "0",
				beginTime: '',
				endTime: '',
				content: '',
				msgType: '',
				itemJson:'',
				attachId:''
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				title: [
					{required: true, message: '投票标题不能为空', trigger: 'blur'}
				],
				sendDeptId: [
					{required: true, message: '发布部门不能为空', trigger: 'blur'}
				],
				beginTime: [
					{required: true, message: '开始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '结束时间不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.itemJson = JSON.stringify(this.itemJson);
					let res;
					if (this.mode == "add") {
						res = await this.$API.info.vote.insertVote.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.info.vote.updateVote.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.voteId = data.voteId
			this.form.title = data.title
			this.form.voteType = data.voteType
			this.form.sendDeptId = data.sendDeptId
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.isAnonymous = data.isAnonymous
			this.form.readRes = data.readRes
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.content = data.content
			this.form.msgType = data.msgType
			this.form.attachId = data.attachId
			if(data.itemJson!=undefined)
			{
				this.itemJson = JSON.parse(data.itemJson);
			}
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
